package com.example.song.wlms.homefragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.song.wlms.R;
import com.example.song.wlms.adapter.FineGridViewAdapter;
import com.example.song.wlms.adapter.HotGridViewAdapter;
import com.example.song.wlms.adapter.RecommendbannerAdapter;
import com.example.song.wlms.adapter.SpecialGridAdapter;
import com.example.song.wlms.bean.ReconmmendData;
import com.example.song.wlms.widget.GlideYuanjiaoTransform;
import com.example.song.wlms.widget.MyGridView;
import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecommendFragment extends Fragment {
    private Timer timer;
    private HotGridViewAdapter hotGridViewAdapter;
    private TextView textView;
    private SpecialGridAdapter specialGridAdapter;
    private List<ReconmmendData.DataBean.SPECIALBean> specialBeanList = new ArrayList<>();
    private FineGridViewAdapter fineGridViewAdapter;
    private List<ReconmmendData.DataBean.FINEBean> fineBeen = new ArrayList<>();
    private List<String> fineImgelist = new ArrayList<>();
    private String url_path = "http://api.viimoo.cn/publics/indexRecommend";
    private ViewPager viewPager;
    private RecommendbannerAdapter recommendbannerAdapter;
    private List<ReconmmendData.DataBean.BANNERBean> bannerBeanList = new ArrayList<>();
    private List<ReconmmendData.DataBean.HOTBean> hotBeanList = new ArrayList<>();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int currentItem = viewPager.getCurrentItem();
            int count = recommendbannerAdapter.getCount();
            if (currentItem == (count - 1)) {
                viewPager.setCurrentItem(0);
            } else {
                currentItem++;
                viewPager.setCurrentItem(currentItem);
            }
            textView.setText(viewPager.getCurrentItem() + 1 + "/" + count);
        }
    };
    private MyGridView hotGridView;
    private TextView titleView;
    private TextView descText;
    private ImageView image1;
    private ImageView image2;
    private ImageView image3;
    private TextView dianzan;
    private TextView pinglun;
    private TextView time;
    private MyGridView finGridView;
    private MyGridView zhuanGrid;


    public RecommendFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recommend, container, false);
        initView(view);
        getData();
        return view;
    }

    private void getData() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(url_path).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                Gson gson = new Gson();
                ReconmmendData reconmmendData = gson.fromJson(response.body().string(), ReconmmendData.class);
                bannerBeanList.addAll(reconmmendData.getData().getBANNER());
                hotBeanList.addAll(reconmmendData.getData().getHOT());
                fineImgelist.addAll(reconmmendData.getData().getFINE().get(0).getImg());
                specialBeanList.addAll(reconmmendData.getData().getSPECIAL());
                fineBeen.add(reconmmendData.getData().getFINE().get(1));
                fineBeen.add(reconmmendData.getData().getFINE().get(2));
                final String title = reconmmendData.getData().getFINE().get(0).getTitle();
                final String desc = reconmmendData.getData().getFINE().get(0).getDesc();
                final String times = reconmmendData.getData().getFINE().get(0).getPushtime();
                final String dianzans = reconmmendData.getData().getFINE().get(0).getComment_totle();
                final String pingluns = reconmmendData.getData().getFINE().get(0).getPraise_totle();
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        recommendbannerAdapter = new RecommendbannerAdapter(bannerBeanList, getContext());
                        viewPager.setAdapter(recommendbannerAdapter);
                        viewPager.setOffscreenPageLimit(4);
                        timer = new Timer();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                handler.sendEmptyMessageDelayed(1, 2000);
                            }
                        }, 2000, 2000);
                        hotGridViewAdapter = new HotGridViewAdapter(hotBeanList, getContext());
                        hotGridView.setAdapter(hotGridViewAdapter);
                        titleView.setText(title);
                        Glide.with(getContext()).load(fineImgelist.get(0)).transform(new GlideYuanjiaoTransform(getContext())).into(image1);
                        Glide.with(getContext()).load(fineImgelist.get(1)).transform(new GlideYuanjiaoTransform(getContext())).into(image2);
                        Glide.with(getContext()).load(fineImgelist.get(2)).transform(new GlideYuanjiaoTransform(getContext())).into(image3);
                        descText.setText(desc);
                        time.setText(times);
                        dianzan.setText(dianzans);
                        pinglun.setText(pingluns);
                        fineGridViewAdapter = new FineGridViewAdapter(fineBeen, getContext());
                        finGridView.setAdapter(fineGridViewAdapter);
                        specialGridAdapter = new SpecialGridAdapter(specialBeanList, getContext());
                        zhuanGrid.setAdapter(specialGridAdapter);
                    }
                });
            }
        });
    }

    private void initView(View view) {
        viewPager = ((ViewPager) view.findViewById(R.id.recommend_viewPager));
        textView = ((TextView) view.findViewById(R.id.shumu));
        hotGridView = ((MyGridView) view.findViewById(R.id.HotGrid));
        titleView = ((TextView) view.findViewById(R.id.title));
        descText = ((TextView) view.findViewById(R.id.desc));
        image1 = ((ImageView) view.findViewById(R.id.image1));
        image2 = ((ImageView) view.findViewById(R.id.image2));
        image3 = ((ImageView) view.findViewById(R.id.image3));
        dianzan = ((TextView) view.findViewById(R.id.dianzan));
        pinglun = ((TextView) view.findViewById(R.id.pinglun));
        time = ((TextView) view.findViewById(R.id.time));
        finGridView = ((MyGridView) view.findViewById(R.id.fine));
        zhuanGrid = ((MyGridView) view.findViewById(R.id.zhuanGrid));
    }

}
